package cn.xshi.sys.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xshi.sys.dao.SysPostDao;
import cn.xshi.sys.form.SysPostForm;
import cn.xshi.sys.service.SysPostService;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.log.client.service.LogsUtil;
import cn.xshi.sys.model.SysPost;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * @Desc 用户岗位
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class SysPostServiceImpl extends BaseService implements SysPostService {

	@Resource
	SysPostDao sysPostDao;

	@Resource
	LogsUtil logsUtil;

	/**
	 * 分页
	 * @param sysPostForm
	 * @return
	 */
	public List<SysPost> getSysPostListByCondition(SysPostForm sysPostForm){
		try {
			return sysPostDao.getSysPostListByCondition(sysPostForm);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 查询对象
	 * @param id
	 * @return
	 */
	public SysPost getSysPostById(String id){
		try {
			return sysPostDao.getSysPostById(id);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 添加
	 * @param sysPost
	 * @return
	 */
	public int addSysPost(SysPost sysPost){
		int i = 0;
		try {
			sysPost.setCreateId(getCurrentAccountId());
			sysPost.setCreateTime(getDate());
			i = sysPostDao.addSysPost(sysPost);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("岗位业务类", "添加", "添加岗位成功");
		} catch (Exception e) {
			logsUtil.aBLogs("岗位业务类", "添加", "添加岗位失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 修改
	 * @param sysPost
	 * @return
	 */
	public int updateSysPost(SysPost sysPost){
		int i = 0;
		try {
			sysPost.setUpdateId(getCurrentAccountId());
			sysPost.setUpdateTime(getDate());
			i = sysPostDao.updateSysPost(sysPost);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("岗位业务类", "修改", "修改岗位成功");
		} catch (Exception e) {
			logsUtil.aBLogs("岗位业务类", "修改", "修改岗位失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 删除
	 * @param condition
	 * @return
	 */
	public int delSysPost(Map<String,Object> condition){
		int i = 0;
		try {
			String[] idList = (String[])condition.get("id");
			//验证下级岗位是否存在
			for(String id:idList){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("parentId", id);
				if(!sysPostDao.getSysPostListChild(map).isEmpty()){
					logsUtil.aBLogs("岗位业务类", "删除", "执行删除岗位，编号【"+id+"】存在下级岗位，不能删除");
					return 0;
				}
			}
			i = sysPostDao.delSysPost(condition);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("岗位业务类", "删除", "删除岗位成功");
		} catch (Exception e) {
			logsUtil.aBLogs("岗位业务类", "删除", "删除岗位失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 岗位根目录集合
	 * @return
	 */
	public List<SysPost> getSysPostInfoList(Map<String,Object> condition){
		return sysPostDao.getSysPostInfoList(condition);
	}

	/**
	 * 查找子集合
	 * @return
	 */
	public List<SysPost> getSysPostListChild(Map<String,Object> condition){
		try {
			return sysPostDao.getSysPostListChild(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 查找所有集合
	 * @return
	 */
	public List<SysPost> getSysPostListAll(Map<String,Object> condition){
		try {
			return sysPostDao.getSysPostListAll(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 根据各种情况查找集合不分页（流程设计器中处理组 发起组等使用）
	 * @param condition
	 * @return
	 */
	public List<SysPost> getSysPostList(Map<String,Object> condition){
		return sysPostDao.getSysPostList(condition);
	}

	/**
	 * 非根岗位全部集合
	 * @param condition
	 * @return
	 */
	public List<SysPost> getSysPostInfoUnRootList(Map<String,Object> condition){
		return sysPostDao.getSysPostInfoUnRootList(condition);
	}
}
